package io.kiki.sba.registry.server.data_store.remoting.metaserver.handler;

import io.kiki.sba.registry.api.Channel;
import io.kiki.sba.registry.common.model.Node.NodeType;
import io.kiki.sba.registry.common.model.Response;
import io.kiki.sba.registry.common.model.metaserver.ProvideData;
import io.kiki.sba.registry.common.model.metaserver.ProvideDataChangeEvent;
import io.kiki.sba.registry.server.shared.meta.MetaServerService;
import io.kiki.sba.registry.server.shared.providedata.ProvideDataProcessor;
import io.kiki.sba.registry.server.shared.remoting.AbstractClientHandler;
import io.kiki.sba.registry.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


public class NotifyProvideDataChangeHandler extends AbstractClientHandler<ProvideDataChangeEvent> {

    @Autowired
    private MetaServerService metaServerService;

    @Autowired
    private ProvideDataProcessor provideDataProcessorManager;

    @Autowired
    private ThreadPoolExecutor metaNodeExecutor;

    @Override
    protected NodeType getConnectNodeType() {
        return NodeType.central_control;
    }

    @Override
    public void checkParam(ProvideDataChangeEvent request) {
        StringUtil.checkNotBlank(request.getDataInfoId(), "request.dataInfoId");
    }

    @Override
    public Object doHandle(Channel channel, ProvideDataChangeEvent request) {
        String dataInfoId = request.getDataInfoId();
        ProvideData provideData = metaServerService.getProvideData(dataInfoId);
        provideDataProcessorManager.processData(provideData);
        return null;
    }

    @Override
    public Class interest() {
        return ProvideDataChangeEvent.class;
    }

    @Override
    public Response buildFailedResponse(String msg) {
        return Response.buildFailedResponse(msg);
    }


    @Override
    public Executor getExecutor() {
        return metaNodeExecutor;
    }
}
